package com.czhoujunlv.drug.State;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.czhoujunlv.drug.Bean.Dingdan;
import com.czhoujunlv.drug.Bean.Dingdan2;
import com.czhoujunlv.drug.Bean.Dingdan3;
import com.czhoujunlv.drug.Bean.DingdanState;
import com.czhoujunlv.drug.R;
import com.czhoujunlv.drug.fragment.HeadFragment;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Payment extends AppCompatActivity {

    RecyclerView recyclerView;
    TextView title;
    ImageView back;
    String text;
    String res;
    List<DingdanState> dingdan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);

        init();
        Intent intent = getIntent();
        text = intent.getStringExtra("text");
        title.setText(text);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Thread thread = new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    if(text.equals("待付款")) {
                        res = post("http://192.168.43.138:8080/index/getNoPayOrder/" + 1);
                        //dingdan.clear();
                        Log.i("状态：","未付款");
                    }else if(text.equals("待发货")){
                        res = post("http://192.168.43.138:8080/index/getPayedOrder/" + 1);
                        //dingdan.clear();
                        Log.i("状态：","待发货");
                    }else if(text.equals("待发货")){
                        res = post("http://192.168.43.138:8080/index/getOnTranslationOrder/" + 1);
                        //dingdan.clear();
                        Log.i("状态：","待发货");
                    }else{
                        res = post("http://192.168.43.138:8080/index/getFinishOrder/" + 1);
                        //dingdan.clear();
                        Log.i("状态：","已收货");
                    }
                    Gson gson = new Gson();
                    Dingdan3 dingdan3 = gson.fromJson(res,Dingdan3.class);
                    dingdan = new ArrayList<>();
                    dingdan.clear();
                    for(int i=0;i<dingdan3.getData().size();i++){
                        DingdanState dingdanState = new DingdanState();
                        dingdanState.img = dingdan3.getData().get(i).getImg();
                        dingdanState.name = dingdan3.getData().get(i).getName();
                        dingdanState.description = dingdan3.getData().get(i).getDescription();
                        dingdanState.number = dingdan3.getData().get(i).getNumber();
                        dingdan.add(dingdanState);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Payment.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.setAdapter(new DingdanAdapter());
                        recyclerView.setLayoutManager(new LinearLayoutManager(Payment.this));
                    }
                });
            }
        };
        thread.start();



    }

    public class DingdanAdapter extends RecyclerView.Adapter<DingdanHolder>{

        @NonNull
        @Override
        public DingdanHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(Payment.this).inflate(R.layout.item_dingdan,parent,false);
            DingdanHolder dingdanHolder = new DingdanHolder(view);
            return dingdanHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull DingdanHolder holder, int position) {
            holder.name.setText(dingdan.get(position).name);
            holder.des.setText(dingdan.get(position).description);
            holder.number.setText(String.valueOf(dingdan.get(position).number));
            Glide.with(Payment.this).load(dingdan.get(position).img).into(holder.img);
        }

        @Override
        public int getItemCount() {
            return dingdan.size();
        }
    }

    public class DingdanHolder extends RecyclerView.ViewHolder{
        TextView name,des,number;
        ImageView img;
        public DingdanHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.imageView15);
            name = itemView.findViewById(R.id.textView29);
            des = itemView.findViewById(R.id.textView10);
            number = itemView.findViewById(R.id.textView);
        }
    }
    private void init(){
        title = findViewById(R.id.title);
        recyclerView = findViewById(R.id.recyclerView);
        back = findViewById(R.id.back);
    }
    //不带token
    public String post (String url) throws IOException {
        RequestBody body = RequestBody.create(MediaType.parse("application/json"),"1");
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = HeadFragment.client.newCall(request).execute();
        if (response.isSuccessful()) {
            String res = response.body().string();
            Log.i("返回状态结果：", res);
            return res;
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }
}